package com.teatrace.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.teatrace.model.base.Storage;
import com.teatrace.model.produce.MaterialPackage;
import com.teatrace.service.storage.StorageService;
import com.teatrace.service.storage.impl.StorageServiceImpl;


public class StorageAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static final Logger log = Logger.getLogger(StorageAction.class);
	
	protected StorageService storageService = null;
	
	private HttpServletRequest request;
	private int storageId;
	private int packageId;
	
	public StorageAction(){
		storageService = new StorageServiceImpl();
		request =  ServletActionContext.getRequest();
	}
	
	/**
	 * 查出 可出库的
	 */
	public String listCanOut(){
		log.debug("查出可以出库的原料包");
		try {
			int id = 0;
			if(request.getParameter("id") != null)
				id = Integer.parseInt(request.getParameter("id"));
			log.debug(id);
			if(id != 0){
				storageId = id;
			}
			List<MaterialPackage> materialPackageList = storageService.listByCriteria(MaterialPackage.class, 0, "storage.id", storageId, "status", 1);
			request.setAttribute("materialPackageList", materialPackageList);
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ERROR;
		}
	}

	/**
	 * 入库
	 * @return
	 */
	public String putInStorage(){
		log.debug("入库操作");
		MaterialPackage materialPackage = null;
		try {
			Storage storage = (Storage) storageService.getById(Storage.class,storageId);
			materialPackage = (MaterialPackage) storageService.getById(MaterialPackage.class, packageId);
			if(storageId == 1){
				log.debug("原料包入库");
				log.debug(packageId);
				materialPackage.setStorage(storage);
			}else if(storageId == 2){
				//半成品 入库
			}else if(storageId == 3){
				//商品入库
			}
			storageService.update(materialPackage);
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.debug(e.getMessage());
			if(materialPackage.getStatus() == 1){
				request.setAttribute("cause", "原料包已经在库中");
				return ERROR;
			}
			if(materialPackage.getStatus() == 2){
				request.setAttribute("cause", "原料包已经不在库中");
				return ERROR;
			}
			request.setAttribute("cause", e.getMessage());
			return ERROR;
		}
	}
	
	/**
	 * 出库操作
	 * @return
	 */
	public String outOfStorage(){
		log.debug("出库");
		try {
			MaterialPackage materialpacka = (MaterialPackage) storageService.getById(MaterialPackage.class, packageId);
			materialpacka.setStatus(2);
			storageService.update(materialpacka);
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.debug(e.getMessage());
			return ERROR;
		}
	}
	
	public int getStorageId() {
		return storageId;
	}

	public void setStorageId(int storageId) {
		this.storageId = storageId;
	}

	public void setPackageId(int packageId) {
		this.packageId = packageId;
	}

	public int getPackageId() {
		return packageId;
	}
	
}
